package com.xuan.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.xuan.base.constant.MQConstant;
import com.xuan.base.constant.RedisConstant;
import com.xuan.oath.client.api.model.entity.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author Lenovo
 * @description: TODO 资源与角色匹配关系管理业务类
 * @date 2021/7/22 16:32
 */
@Slf4j
@Service
public class ResourceServiceImpl {

    private Map<String, List<String>> resourceRolesMap;


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //加载Bean时加载此方法
    @PostConstruct
    public void initData() {
        //TODO 增加逻辑 先判断redis中是否有值，如没有则向后台发送消息进行redis缓存
        // TODO 注：相关资源与角色匹配需要在增删改中进行redis缓存重置
        if (CollUtil.isEmpty(redisTemplate.opsForHash().keys(RedisConstant.RESOURCE_ROLES_MAP))){
            log.info("===向resource发送更新资源缓存消息===");
            rabbitTemplate.convertAndSend(MQConstant.ADD_REDIS_EXCHANGE,MQConstant.ADD_REDIS_BINDING_URL,1);
        }
        log.info("没有向资源服务发送消息，redis已存键为=={}",redisTemplate.opsForHash().keys(RedisConstant.RESOURCE_ROLES_MAP));
    }
}
